from django.contrib import admin
from django.utils.html import format_html
from .models import Archive,ArchiveLinkedFile,SignedDoc,SignedDocLinkedFile,PostedDoc,PostedDocLinkedFile,ReceivedDoc,ReceivedDocLinkedFile,PhoneRecord,PhoneRecordLinkedFile

@admin.display(description='预览文件')
def create_preview_html(self,obj):
    file_path=str(obj.file)
    if not file_path:
        return ''
    # PreviewTemplate='<a href="/viewer/{}">{}</a>'
    PreviewTemplate='''<button onclick="fetch('/viewer/{}');alert('请等待文件打开...');event.preventDefault();">{}</button>'''
    
    return format_html(PreviewTemplate,file_path,file_path.split('/')[-1])

# class Media:
#     css={
#         'all':('file.css',)
#     }


def CreateLinkedFileAdmin(app:str,model):
    return type(app+'LinkedFileAdmin',(admin.StackedInline,),{
        'model':model,
        'extra':1,
        'readonly_fields':['preview'],
        'preview':create_preview_html,
        # 'Media':Media,
    })


# class ArchiveLinkedFileAdmin(admin.StackedInline):
#     model=ArchiveLinkedFile
#     extra=1
#     readonly_fields=['preview']
    
#     @admin.display(description='预览链接')
#     def preview(self,obj):
#         return create_preview_html(obj)

class ArchiveAdmin(admin.ModelAdmin):
    fields=['title','type','date','comment']
    list_display=['idx','title','type','date','comment','file_count']
    list_display_links=['title']
    list_filter=['type','date']
    search_fields=['title']
    search_help_text='检索档案标题'

    # inlines=[ArchiveLinkedFileAdmin]
    inlines=[CreateLinkedFileAdmin('Archive',ArchiveLinkedFile)]
    
    @admin.display(description='文件数量')
    def file_count(self,obj):
        return ArchiveLinkedFile.objects.filter(owner=obj).count()

admin.site.register(Archive,ArchiveAdmin)


# class SignedDocLinkedFileAdmin(admin.StackedInline):
#     model=SignedDocLinkedFile
#     extra=1

class SignedDocAdmin(admin.ModelAdmin):
    fields=['doc_id','title','date','comment']
    list_display=['idx','doc_id','title','date','comment','file_count']
    list_display_links=['doc_id','title']
    list_filter=['date']
    search_fields=['title']
    search_help_text='检索标题'

    # inlines=[SignedDocLinkedFileAdmin]
    inlines=[CreateLinkedFileAdmin('SignedDoc',SignedDocLinkedFile)]

    
    @admin.display(description='文件数量')
    def file_count(self,obj):
        return SignedDocLinkedFile.objects.filter(owner=obj).count()


admin.site.register(SignedDoc,SignedDocAdmin)


# class PostedDocLinkedFileAdmin(admin.StackedInline):
#     model=PostedDocLinkedFile
#     extra=1

class PostedDocAdmin(admin.ModelAdmin):
    fieldsets=[
        ('公文信息',{
            'fields':['doc_id','title','classification']
        }),
        ('发文记录',{
            'fields':['sender','post_date']
        })
    ]

    list_display=['idx','doc_id','title','classification','sender','post_date','file_count']
    list_display_links=['doc_id','title']
    list_filter=['classification','sender','post_date']
    search_fields=['doc_id','title']
    search_help_text='检索公文编号或标题'
    # inlines=[PostedDocLinkedFileAdmin]
    inlines=[CreateLinkedFileAdmin('PostedDoc',PostedDocLinkedFile)]


    @admin.display(description='文件数量')
    def file_count(self,obj):
        return PostedDocLinkedFile.objects.filter(owner=obj).count()


admin.site.register(PostedDoc,PostedDocAdmin)


# class ReceivedDocLinkedFileAdmin(admin.StackedInline):
#     model=ReceivedDocLinkedFile
#     extra=1

class ReceivedDocAdmin(admin.ModelAdmin):
    fieldsets=[
        ('公文信息',{
            'fields':['doc_id','title','classification','sender']
        }),
        ('收文记录',{
            'fields':['recipient','receive_date']
        })
    ]

    list_display=['idx','doc_id','title','classification','sender','recipient','receive_date','file_count']
    list_display_links=['doc_id','title']
    list_filter=['classification','sender','recipient','receive_date']
    search_fields=['doc_id','title']
    search_help_text='检索公文编号或标题'
    # inlines=[ReceivedDocLinkedFileAdmin]
    inlines=[CreateLinkedFileAdmin('ReceivedDoc',ReceivedDocLinkedFile)]

    
    @admin.display(description='文件数量')
    def file_count(self,obj):
        return ReceivedDocLinkedFile.objects.filter(owner=obj).count()

admin.site.register(ReceivedDoc,ReceivedDocAdmin)


# class PhoneRecordLinkedFileAdmin(admin.StackedInline):
#     model=PhoneRecordLinkedFile
#     extra=1

class PhotoRecordAdmin(admin.ModelAdmin):
    fields=['title','date','comment']
    list_display=['idx','title','date','comment','file_count']
    list_display_links=['title']
    list_filter=['date']
    search_fields=['title']
    search_help_text='检索记录标题'

    # inlines=[PhoneRecordLinkedFileAdmin]
    inlines=[CreateLinkedFileAdmin('PhoneRecord',PhoneRecordLinkedFile)]

    
    @admin.display(description='文件数量')
    def file_count(self,obj):
        return PhoneRecordLinkedFile.objects.filter(owner=obj).count()


admin.site.register(PhoneRecord,PhotoRecordAdmin)